package com.example.ivideo_2211a.search


import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.ivideo_2211a.R


class SearchHotAdapter(private val data: List<String>) :
    RecyclerView.Adapter<SearchHotAdapter.ItemViewHolder>() {
    private lateinit var onItemClickListener: OnItemClickListener
    interface OnItemClickListener {
        fun onItemClick(position: Int, data: String)
    }
    class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_hot_search_tag = itemView.findViewById<TextView>(R.id.tv_hot_search_tag)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        onItemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val itemViewHolder = ItemViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_hot_search_tag, parent, false)
        )
        Log.d("SearchHotAdapter", "onCreateViewHolder: 视图创建完成")
        return itemViewHolder
    }


    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.tv_hot_search_tag.text = data[position]
        holder.tv_hot_search_tag.setOnClickListener {
            onItemClickListener.onItemClick(position, data[position])
        }

    }

    override fun getItemCount() :Int{
        val size = data.size
        Log.d("hotAdapter size", size.toString())
        return   size
    }

}